Design guideline for saving big byte stream in c# [migrated]

Posted by Praveen on Programmers See other posts from Programmers or by Praveen
Published on 2012-11-05T22:19:30Z Indexed on 2012/11/05 23:17 UTC
Read the original article Hit count: 295

Filed under:

I have an application where I am receiving big byte array very fast around per 50 miliseconds.

The byte array contains some information like file name etc. The data (byte array ) may come from several sources.

Each time I receive the data, I have to find the file name and save the data to that file name.

I need some guide lines to how should I design it so that it works efficient.

Following is my code...

public class DataSaver
{
    private static Dictionary<string, FileStream> _dictFileStream;

    public static void SaveData(byte[] byteArray)
    {
        string fileName = GetFileNameFromArray(byteArray);
        FileStream fs = GetFileStream(fileName);
        fs.Write(byteArray, 0, byteArray.Length);
    }

    private static FileStream GetFileStream(string fileName)
    {
        FileStream fs;
        bool hasStream = _dictFileStream.TryGetValue(fileName, out fs);
        if (!hasStream)
        {
            fs = new FileStream(fileName, FileMode.Append);
            _dictFileStream.Add(fileName, fs);
        }
        return fs;
    }

    public static void CloseSaver()
    {
        foreach (var key in _dictFileStream.Keys)
        {
            _dictFileStream[key].Close();
        }
    }
}

How can I improve this code ? I need to create a thread maybe to do the saving.

© Programmers or respective owner

Related posts about c#